package com.medicine.web.controller.manage;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.medicine.manage.domain.Order;
import com.medicine.manage.domain.OrderProcess;
import com.medicine.manage.service.IOrderProcessService;
import com.medicine.manage.service.IOrderService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.medicine.common.annotation.Log;
import com.medicine.common.core.controller.BaseController;
import com.medicine.common.core.domain.AjaxResult;
import com.medicine.common.enums.BusinessType;
import com.medicine.manage.domain.Boiling;
import com.medicine.manage.service.IBoilingService;
import com.medicine.common.utils.poi.ExcelUtil;
import com.medicine.common.core.page.TableDataInfo;

/**
 * 煎药Controller
 *
 * @author admin
 * @date 2024-10-14
 */
@RestController
@RequestMapping("/manage/boiling")
public class BoilingController extends BaseController
{
    @Autowired
    private IBoilingService boilingService;

    @Autowired
    private IOrderProcessService orderProcessService;

    @Autowired
    private IOrderService orderService;


    private final Long TYPE = 3L;

    /**
     * 查询煎药列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Boiling boiling)
    {
        startPage();
        List<Boiling> list = boilingService.selectBoilingList(boiling);
        return getDataTable(list);
    }

    /**
     * 导出煎药列表
     */
    @Log(title = "煎药", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Boiling boiling)
    {
        List<Boiling> list = boilingService.selectBoilingList(boiling);
        ExcelUtil<Boiling> util = new ExcelUtil<Boiling>(Boiling.class);
        util.exportExcel(response, list, "煎药数据");
    }

    /**
     * 获取煎药详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(boilingService.selectBoilingById(id));
    }

    @GetMapping(value = "/orderId/{id}")
    public AjaxResult getInfoByOrderId(@PathVariable("id") Long id)
    {
        Boiling boiling = new Boiling();
        boiling.setOrderId(id);
        List<Boiling> boilings = boilingService.selectBoilingList(boiling);
        if(boilings.size() == 1){
            return success(boilings.get(0));
        } else {
            return error("不存在ordId[" + id + "]的记录");
        }

    }

    /**
     * 新增煎药
     */
    @Log(title = "煎药", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Boiling boiling)
    {
        OrderProcess orderProcess = orderProcessService.selectOrderProcessByOrderAndType(boiling.getOrderId(), TYPE);
        if(orderProcess!=null){
            boiling.setProcessId(orderProcess.getId());
            orderProcess.setStartTime(new Date());
            orderProcessService.updateOrderProcess(orderProcess);
        }

        // 修改  order 的  pan id
        Order order = new Order();
        order.setId(boiling.getOrderId());
        order.setPanId(boiling.getPanId());
        order.setStatus(TYPE);
        orderService.updateOrder(order);

        return toAjax(boilingService.insertBoiling(boiling));
    }

    /**
     * 修改煎药
     */
    @Log(title = "煎药", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Boiling boiling)
    {
        return toAjax(boilingService.updateBoiling(boiling));
    }

    /**
     * 删除煎药
     */
    @Log(title = "煎药", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(boilingService.deleteBoilingByIds(ids));
    }
}
